package com.hcy.app.data.di

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.hcy.app.data.dao.Song
import com.hcy.app.data.dao.SongDao
import com.hcy.app.screen.cipher.ContactDao
import com.hcy.app.screen.cipher.ContactItem
import com.hcy.app.screen.cipher.NoteDao
import com.hcy.app.screen.cipher.NoteItem
import com.hcy.app.screen.cipher.SecretDao
import com.hcy.app.screen.cipher.SecretItem
import com.hcy.app.screen.relax.RelaxFileInfo
import com.hcy.app.screen.relax.RelaxFileInfoDao
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton


// 模型关联数据库 ，将dao 注册到 entities 数组中
@Database(
    entities = [Song::class,RelaxFileInfo::class, NoteItem::class, SecretItem::class, ContactItem::class],
    version = 4,
    exportSchema = true)
abstract class AppDatabase :RoomDatabase(){
    /**
     * 对应的操作表的dao函数，也是抽象的。
     */
    abstract fun songDao(): SongDao // 方法名建议与接口名一致

    abstract fun relaxFileInfoDao(): RelaxFileInfoDao // 方法名建议与接口名一致

    abstract fun noteDao(): NoteDao

    abstract fun secretDao(): SecretDao

    abstract fun contactDao(): ContactDao

}


@Module
@InstallIn(SingletonComponent::class) // 单例作用域
object DatabaseModule  {
    // 提供 Application Context（Hilt 内置支持，无需实际编写）
    // @Provides 注解仅用于自定义依赖

    @Provides
    @Singleton
    fun provideSongDatabase(@ApplicationContext context: Context): AppDatabase {
        return Room.databaseBuilder(
            context,
            AppDatabase::class.java,
            "miao.db" // 数据库文件名
        )
            .fallbackToDestructiveMigration() // // 清空旧数据
            .build()
    }

    @Provides
    fun provideSongDao(database: AppDatabase): SongDao {
        return database.songDao()
    }

    @Provides
    fun provideRelaxFileInfoDao(database: AppDatabase): RelaxFileInfoDao {
        return database.relaxFileInfoDao()
    }

    @Provides
    fun provideNoteDao(database: AppDatabase): NoteDao{
        return database.noteDao()
    }

    @Provides
    fun provideSecretDao(database: AppDatabase): SecretDao{
        return database.secretDao()
    }

    @Provides
    fun provideContactDao(database: AppDatabase): ContactDao{
        return database.contactDao()
    }

}